package com.example.designpattern.test.testProtected;

import lombok.SneakyThrows;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.IntConsumer;
import java.util.function.UnaryOperator;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public interface MessageBuilder {
    String buildMessage();
}

class Demo02LoggerLambda {
    int a;

    public Demo02LoggerLambda() {
        System.out.println(123);
    }

    public Demo02LoggerLambda(int a) {
        System.out.println(a);
    }

    private static void log(int level, MessageBuilder builder) {
        if (level == 1) {
            System.out.println(builder.buildMessage());
        }
    }

    public static void main(String[] args) throws IOException {
        String msgA = "Hello ";
        String msgB = "world ";
        String msgC = "Java ";
        log(2, () -> msgA + msgB + msgC);

        BufferedReader reader = new BufferedReader(new FileReader("src\\main\\java\\com\\example\\designpattern\\test\\testProtected\\t.txt"));
        Stream<String> lineStream = reader.lines();
        lineStream.forEach(System.out::println);
        reader.close();

        IntStream stream = Arrays.stream(new int[]{1, 2, 3, 4});
        System.out.println(stream);
        Stream<Integer> intStream = Stream.iterate(0, new UnaryOperator<Integer>() {
            @Override
            public Integer apply(Integer integer) {
                System.out.println("hello world");
                return integer + 2;
            }
        }).limit(10);
        Integer[] arr = {1, 4, 2, 7, 32, 67, 23, 43, 6};
        /*Integer[] sort = sort(arr, integers -> {
            int temp = 0;
            for (int i = 0; i < integers.length - 1; i++) {
                for (int j = 0; j < integers.length - i - 1; j++) {
                    if (integers[j] > integers[j + 1]) {
                        temp = integers[j];
                        integers[j] = integers[j + 1];
                        integers[j + 1] = temp;
                    }
                }

            }
            return integers;
        });*/
        Integer[] sort = sort(arr);
        System.out.println("Integer[] sort " + Arrays.toString(sort));
        System.out.println(intStream);
        Consumer<String> tPublisher = s -> System.out.println("1");
        tPublisher.accept("");

        Runnable aNew = Demo02LoggerLambda::new;
        IntConsumer runnable = Demo02LoggerLambda::new;
        runnable.accept(9);
        new Thread(aNew).start();
        intStream.forEach(integer -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(integer);
        });
    }

    static Integer[] sort(Integer[] arr) {
        Function<Integer[], Integer[]> f = integers -> {
            int temp = 0;
            for (int i = 0; i < integers.length - 1; i++) {
                for (int j = 0; j < integers.length - i - 1; j++) {
                    if (integers[j] > integers[j + 1]) {
                        temp = integers[j];
                        integers[j] = integers[j + 1];
                        integers[j + 1] = temp;
                    }
                }

            }
            return integers;
        };
        return f.apply(arr);
    }
}
